package com.chenfan.magic.config.aspect;

import com.chenfan.common.vo.UserVO;
import com.chenfan.magic.entity.IdcOperationLog;
import com.chenfan.magic.service.IdcOperationLogService;
import com.chenfan.magic.service.impl.LogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author liuming
 * @date 2021-10-28 15:29
 */
@Slf4j
@Aspect
@Component
public class LogAspect {

    @Autowired
    private LogService logService;

    @Pointcut("@annotation(com.chenfan.magic.annation.Log)")
    public void pointCut() {
    }

    /**
     * 记录用户操作埋点
     *
     * @param point
     */
    @AfterReturning(pointcut = "pointCut()")
    public void after(JoinPoint point) {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String currentUri = request.getRequestURI();
            String[] currentPaths = currentUri.split("/");
            logService.saveLog(currentPaths[currentPaths.length - 1]);
        } catch (Exception e) {
            log.error("LogAspect.after error:{}", e);
        }
    }
}
